﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DemoQLKhoSX.DatabaseAccessLayer;
using DemoQLKhoSX.BusinessLogicLayer;
using DemoQLKhoSX.EntityClasses;

namespace DemoQLKhoSX.PresentationLayer
{
    public partial class frmHang : Form
    {
        #region "Variables"
        DataTable dtbHang = new DataTable();
        HangBLO _HangB;
        HangEntity _Hang;
        string _strID;
        #endregion
        public frmHang()
        {
            InitializeComponent();
            _HangB = new HangBLO();
            _Hang = new HangEntity();
            
        }

        private void frmHang_Load(object sender, EventArgs e)
        {
            _Lock();
            _Load();
        }

        public void _Load()
        {
            dtbHang = _HangB.getAllHang();
            dtgHangHoa.DataSource = dtbHang;
            dtgHangHoa.AllowUserToAddRows = false;
            txtIDHang.Text = "";
            txtTenHang.Text = "";
            txtMoTaHang.Text = "";
            txtDonViTinh.Text = "";
            txtSoLuongTon.Text = "";
            txtNguong.Text = "";
            txtPhanLoai.Text = "";
            txtNguonGoc.Text = "";
            txtSize.Text = "";
            txtChatLieu.Text ="";
            txtLoai.Text = "";
            
        }

        public void _Lock()
        {
            txtIDHang.Enabled = false;
            txtTenHang.Enabled = false;
            txtMoTaHang.Enabled = false;
            txtDonViTinh.Enabled = false;
            txtSoLuongTon.Enabled = false;
            txtNguong.Enabled = false;
            txtPhanLoai.Enabled = false;
            txtNguonGoc.Enabled = false;
            txtSize.Enabled = false;
            txtChatLieu.Enabled = false;
            txtLoai.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = false;
            btnXoa.Enabled = false;
        }

        public void UnLock()
        {
            txtIDHang.Enabled = true;
            txtTenHang.Enabled = true;
            txtMoTaHang.Enabled = true;
            txtDonViTinh.Enabled = true;
            txtSoLuongTon.Enabled = true;
            txtNguong.Enabled = true;
            txtPhanLoai.Enabled = true;
            txtNguonGoc.Enabled = true;
            txtSize.Enabled = true;
            txtChatLieu.Enabled = true;
            txtLoai.Enabled = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
        }

        public void _Validate()
        {
            _HangB._HangE.IDHang = txtIDHang.Text;
            _HangB._HangE.TenHang = txtTenHang.Text;
            _HangB._HangE.DonViTinh = txtDonViTinh.Text;
            _HangB._HangE.SoLuongTon = Int32.Parse(txtSoLuongTon.Text);
            _HangB._HangE.Nguong = Int32.Parse(txtNguong.Text);
            _HangB._HangE.PhanLoai = txtPhanLoai.Text;
            _HangB._HangE.NguonGoc = txtNguonGoc.Text;
           // _HangB._HangE.Size = Int32.Parse(txtSize.Text);
            _HangB._HangE.ChatLieu = txtChatLieu.Text;
            _HangB._HangE.Loai = txtLoai.Text;
            _HangB._HangE.MoTaHang = txtMoTaHang.Text;
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            txtIDHang.Enabled = true;
            txtTenHang.Enabled = true;
            txtMoTaHang.Enabled = true;
            txtDonViTinh.Enabled = true;
            txtSoLuongTon.Enabled = true;
            txtNguong.Enabled = true;
            txtPhanLoai.Enabled = true;
            txtNguonGoc.Enabled = true;
            txtSize.Enabled = true;
            txtChatLieu.Enabled = true;
            txtLoai.Enabled = true;
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtIDHang.Enabled == false)
            {
                _Validate();
                _HangB.UpdateHangB();
                _Load();
            }
            else
            {
                _Validate();
                _HangB.InsertHangB();
                _Load();
            }
        }

        private void dtgHangHoa_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _strID = txtIDHang.Text;
            UnLock();
            txtIDHang.Enabled = false;
            if (e.RowIndex >= 0)
            {
                txtIDHang.Text = dtgHangHoa.Rows[e.RowIndex].Cells["IDHang"].Value.ToString();
                txtTenHang.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Tenhang"].Value.ToString();
                txtMoTaHang.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Motahang"].Value.ToString();
                txtDonViTinh.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Donvitinh"].Value.ToString();
                txtSoLuongTon.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Soluongton"].Value.ToString();
                txtNguong.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Nguong"].Value.ToString();
                txtPhanLoai.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Phanloai"].Value.ToString();
                txtNguonGoc.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Nguongoc"].Value.ToString();
                txtSize.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Size"].Value.ToString();
                txtChatLieu.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Chatlieu"].Value.ToString();
                txtLoai.Text = dtgHangHoa.Rows[e.RowIndex].Cells["Loai"].Value.ToString();
                
               
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (_strID == "")
                MessageBox.Show("chưa chọn mã hàng muốn xóa");
            else
            {
                _HangB.DeleteHangB(_strID);
            }
        }
    }
}

